// JavaScript Document
var map;
window.onload=function() {
	init();
}

function getBusResult(){
	var go_p=$("#search_go").val();
	var to_p=$("#search_to").val();
	
	var transit = new BMap.TransitRoute(map, {
		renderOptions: {map: map, panel: "r-result"}
	});
	transit.search(go_p, to_p);
}


//初始化地图
function init() {
	map = new BMap.Map("allmap");
	map.centerAndZoom("衡阳市");
	map.enableScrollWheelZoom();
	map.enableInertialDragging();
	map.enableContinuousZoom();
	add_control();
	add_search();
}

function add_control(){
	var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左上角，添加比例尺
	var top_left_navigation = new BMap.NavigationControl();  //左上角，添加默认缩放平移控件

	//添加控件和比例尺
	map.addControl(top_left_control);        
	map.addControl(top_left_navigation);     
}

function add_search(){
	// 定义一个控件类,即function
	function ZoomControl(){
	  // 默认停靠位置和偏移量
	  this.defaultAnchor = BMAP_ANCHOR_TOP_RIGHT;
	  this.defaultOffset = new BMap.Size(20, 20);
	}

	// 通过JavaScript的prototype属性继承于BMap.Control
	ZoomControl.prototype = new BMap.Control();

	ZoomControl.prototype.initialize = function(map){
		// 创建一个DOM元素
		var div = document.createElement("div");
		
		// 设置样式
		div.style.width = "380px";
		div.style.height = "40px";
		div.style.lineHeight = "40px";
		div.style.backgroundColor = "white";
		
		var go = document.createElement("input");
		go.type = "text";
		go.id = "search_go";
		go.placeholder = "起点";
		go.style.marginLeft = "10px";
		go.style.paddingLeft = "5px";
		go.style.height = "24px";
		go.style.width = "100px";
		
		var to = document.createElement("input");
		to.type = "text";
		to.id = "search_to";
		to.placeholder = "终点";
		to.style.marginLeft = "5px";
		to.style.paddingLeft = "5px";
		to.style.height = "24px";
		to.style.width = "100px";
		
		var btn = document.createElement("input");
		btn.type = "button";
		btn.id = "search_btn";
		btn.value = "搜索";
		btn.style.marginLeft = "10px";
		btn.style.height = "30px";
		btn.style.width = "50px";
		btn.onclick = getBusResult;
		
		var cls = document.createElement("input");
		cls.type = "reset";
		cls.value = "清空";
		cls.style.marginLeft = "10px";
		cls.style.height = "30px";
		cls.style.width = "50px";
		
		var form = document.createElement("form");
		form.action = "";
		form.method = "post";
		
		form.appendChild(go);
		form.appendChild(document.createTextNode(" →"));
		form.appendChild(to);
		form.appendChild(btn);
		form.appendChild(cls);
		
		div.appendChild(form);
		// 添加DOM元素到地图中
		map.getContainer().appendChild(div);
		// 将DOM元素返回
		return div;
}
	// 创建控件
	var myZoomCtrl = new ZoomControl();
	// 添加到地图当中
	map.addControl(myZoomCtrl);
}


